package com.sb.parse.impl;

import com.alibaba.fastjson.JSONObject;
import com.sb.domain.*;
import com.sb.entity.SBContext;
import com.sb.net.Client;
import com.sb.parse.ISBService;
import com.sb.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wujunzhou on 2017/10/27.
 */
@Service("zheJiangZWService")
public class ZheJiangZWService implements ISBService {
    private  final Logger logger= LoggerFactory.getLogger(ZheJiangZWService.class);

    @Override
    public void parseAccount(SBContext sbContext) {
        Account account = sbContext.getAccount();
        account.setUserId(sbContext.getUserId());
        account.setUserSource(sbContext.getUserSource());
        if (sbContext.getContent() == null){
            logger.info("content is null");
            return;
        }
        JSONObject json = JSONObject.parseObject(sbContext.getContent());
        if(!json.getString("status").contains("200")){
            logger.info("non 200 query fail ");
            return;
        }
        json.getJSONArray("data").forEach(data -> {
            JSONObject data1 = (JSONObject) data;
            data1.forEach((k, v) -> {
                if (k.contains("aac004")) {
                    account.setGender(v.toString());
                } else if (k.contains("aac003")) {
//                double base = Double.valueOf(v.toString()) * 100;
                    account.setName(v.toString());
                } else if (k.contains("aab301")) {
                    account.setAddress(v.toString());
                } else if (k.contains("aab004")) {
//                    account.setCompany(v.toString());
                } else if (k.contains("aac002")) {
                    account.setCustomerNo(v.toString());

                }
            });
        });


    }

    @Override
    public void parseFlow(SBContext sbContext) {
        List<Flow> list = sbContext.getAccount().getFlowList();
        if (sbContext.getContent() == null){
            logger.info("content is null");
            return;
        }
        JSONObject json = JSONObject.parseObject(sbContext.getContent());
        if(!json.getString("status").contains("200")){
            logger.info("non 200 query fail ");
            return;
        }
        String standardName;
        String name = json.getString("name");
        if (name.contains("yljf")){//城乡养老，基本养老
            standardName = "养老";
        }else if (name.contains("ybjf")){//城乡医疗，基本医疗
            standardName = "医疗";
        }else if (name.contains("sbcx_shengyjf")){
            standardName = "生育";
        }else if (name.contains("sbcx_syjf")){
            standardName = "失业";
        }else if (name.contains("sbcx_gsjf")){
            standardName = "工伤";
        }else {
            logger.info("other type :"+name);
            return;
        }
//        String finalStandardName = standardName;
        json.getJSONArray("data").forEach(data -> {
            JSONObject data1 = (JSONObject) data;
            Flow flow = new Flow();
            flow.setStandardName(standardName);
            data1.forEach((k, v) -> {
                if (k.contains("jfny")) {
                    flow.setPaymentMonth(v.toString());
                } else if (k.contains("jfjs")) {
                    flow.setPaymentBase(Utils.YuanToFen(v.toString()));
                } else if (k.contains("grj")) {
                    flow.setPersonAmount(Utils.YuanToFen(v.toString()));
                } else if (k.contains("dzqk")) {
                    flow.setAccountFlag(v.toString());
                }
            });
            list.add(flow);
        });
//        System.out.println(list);
    }

    @Override
    public void parseInsurance(SBContext sbContext) {
        List<Insurance> list = sbContext.getAccount().getInsuranceList();
        if (sbContext.getContent() == null){
            logger.info("content is null");
            return;
        }
        JSONObject json = JSONObject.parseObject(sbContext.getContent());
        if(!json.getString("status").contains("200")){
            logger.info("non 200 query fail ");
            return;
        }
        String standardName;
        String name = json.getString("name");
        if (name.contains("sbcx_grjbxx")){
            standardName = "养老";
        }else if (name.contains("sbcx_ybxxcx")){
            standardName = "医疗";
        }else if (name.contains("sbcx_shengyxx")){
            standardName = "生育";
        }else if (name.contains("sbcx_syjbxx")){
            standardName = "失业";
        }else if (name.contains("sbcx_gsjbxx")){
            standardName = "工伤";
        }else {
            logger.info("other type :"+name);
            return;
        }
        json.getJSONArray("data").forEach(data -> {
            JSONObject data1 = (JSONObject) data;
//            Flow flow = new Flow();
            Insurance insurance = new Insurance();
            insurance.setStandardName(standardName);
            data1.forEach((k, v) ->{
                if (k.contains("aac008")) {
                    insurance.setInsuredStatus(v.toString());
                } else if (k.contains("aab004")) {
//                double base = Double.valueOf(v.toString()) * 100;
                    insurance.setCompany(v.toString());
                }
            });
            list.add(insurance);
        });
    }

    @Override
    public void parseConsume(SBContext sbContext) {
        List<MedicalConsume> list = sbContext.getAccount().getMedicalConsumeList();
        if (sbContext.getContent() == null){
            logger.info("content is null");
            return;
        }
        JSONObject json = JSONObject.parseObject(sbContext.getContent());
        if(!json.getString("status").contains("200")){
            logger.info("non 200 query fail ");
            return;
        }
        json.getJSONArray("data").forEach(data -> {
            JSONObject data1 = (JSONObject) data;
            MedicalConsume medicalConsume = new MedicalConsume();
            data1.forEach((k, v) -> {
                if (k.contains("ale010")) {
                    medicalConsume.setConsumesMonth(v.toString());
                } else if (k.contains("aka130")) {
//                double base = Double.valueOf(v.toString()) * 100;
                    medicalConsume.setConsumesType(v.toString());
                } else if (k.contains("akc264")) {
                    medicalConsume.setConsumesAmount(Utils.YuanToFen(v.toString()));
                } else if (k.contains("ybjj")) {
                    medicalConsume.setAccountPayment(Utils.YuanToFen(v.toString()));
                } else if (k.contains("bke082")) {
                    medicalConsume.setCashPayment(Utils.YuanToFen(v.toString()));
                } else if (k.contains("akb021")){
                    medicalConsume.setMedicalInstitutionName(v.toString());
                }
            });
            list.add(medicalConsume);
        });
    }

    @Override
    public void parseAnnualAccount(SBContext sbContext) {
        List<EndowmentAccount> list = sbContext.getAccount().getEndowmentAccountList();
        if (sbContext.getContent() == null){
            logger.info("content is null");
            return;
        }
        JSONObject json = JSONObject.parseObject(sbContext.getContent());
        if(!json.getString("status").contains("200")){
            logger.info("non 200 query fail ");
            return;
        }
        json.getJSONArray("data").forEach(data -> {
            JSONObject data1 = (JSONObject) data;
            EndowmentAccount endowmentAccount = new EndowmentAccount();
            data1.forEach((k, v) -> {
                if (k.contains("age_last_year")) {
                    endowmentAccount.setYear(v.toString());
                } else if (k.contains("age_last_balance")) {
                    endowmentAccount.setLastYearBalance(Utils.YuanToFen(v.toString()));
                } else if (k.contains("age_last_jfys")) {
                    endowmentAccount.setPaymentMonths(v.toString());
                } else if (k.contains("age_curr_balance")) {
                    endowmentAccount.setPaymentAmount(Utils.YuanToFen(v.toString()));
                } else if (k.contains("age_curr_lx")) {
                    endowmentAccount.setInterest(Utils.YuanToFen(v.toString()));
                } else if (k.contains("age_lj_balance")) {
                    endowmentAccount.setBalance(Utils.YuanToFen(v.toString()));
                } else if (k.contains("age_lj_jfys")){
                    endowmentAccount.setTotalMonths(v.toString());
                }
            });
            list.add(endowmentAccount);
        });
    }

    /**
     * 解析医疗账户余额
     *
     * @param sbContext
     */
    public void parseMedicardBalance(SBContext sbContext) {
        Account account = sbContext.getAccount();
        JSONObject json = JSONObject.parseObject(sbContext.getContent());
//        JSONArray data = json.getJSONArray("data");
        json.getJSONArray("data").forEach(data->{
            JSONObject data1 = (JSONObject) data;
            Double zhze = data1.getDouble("dnzhze") + data1.getDouble("lnzhze");
            account.setMedicareBalance(String.valueOf(Math.round(zhze*100)));
        });


    }

//    public static void main(String[] args) throws JsonProcessingException {
//        ZheJiangZWService zheJiangZWService = new ZheJiangZWService();
//        SBContext sbContext = new SBContext();
//
//        sbContext.setContent("{\"message\":\"操作成功\",\"total\":6,\"status\":\"200\",\"name\":\"sbcx_sheng$sbcx_ylzh\",\"data\":[{\"age_last_balance\":\"8676.19\",\"age_last_year\":\"2016\",\"age_last_jfys\":\"12\",\"age_curr_balance\":\"5504.04\",\"age_curr_lx\":\"233.79\",\"age_lj_balance\":\"14414.02\",\"age_lj_jfys\":\"44\"},{\"age_last_balance\":\"3594.46\",\"age_last_year\":\"2015\",\"age_last_jfys\":\"12\",\"age_curr_balance\":\"4351.68\",\"age_curr_lx\":\"64.38\",\"age_lj_balance\":\"8010.52\",\"age_lj_jfys\":\"32\"},{\"age_last_balance\":\"1308.14\",\"age_last_year\":\"2014\",\"age_last_jfys\":\"12\",\"age_curr_balance\":\"2136.6\",\"age_curr_lx\":\"36.24\",\"age_lj_balance\":\"3480.98\",\"age_lj_jfys\":\"20\"},{\"age_last_balance\":\"132.66\",\"age_last_year\":\"2013\",\"age_last_jfys\":\"7\",\"age_curr_balance\":\"1122.45\",\"age_curr_lx\":\"11.73\",\"age_lj_balance\":\"1266.84\",\"age_lj_jfys\":\"8\"},{\"age_last_balance\":\"128.48\",\"age_last_year\":\"2012\",\"age_last_jfys\":\"0\",\"age_curr_balance\":\"0\",\"age_curr_lx\":\"0\",\"age_lj_balance\":\"128.48\",\"age_lj_jfys\":\"1\"},{\"age_last_balance\":\"0\",\"age_last_year\":\"2011\",\"age_last_jfys\":\"1\",\"age_curr_balance\":\"122.6\",\"age_curr_lx\":\"1.68\",\"age_lj_balance\":\"124.28\",\"age_lj_jfys\":\"1\"}],\"dataExt\":{\"status\":1},\"success\":true}");
//        zheJiangZWService.parseAnnualAccount(sbContext);
//
//        ObjectMapper mapper = new ObjectMapper();
//        String json = mapper.writeValueAsString(sbContext);
//        System.out.println(json);
////        System.out.println("__qssj"+JSON.toJSON(sbContext.getFlowList()));
//
//    }
}
